package com.lc.guessTheWords.util;

import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.widget.FrameLayout;

import com.adsmogo.adapters.AdsMogoCustomEventPlatformEnum;
import com.adsmogo.adview.AdsMogoLayout;
import com.adsmogo.controller.listener.AdsMogoListener;
import com.lc.guessTheWords.MainActivity;
import com.lc.guessTheWords.core.StageManager;
import com.lc.guessTheWords.scene.GameScene;

public class ADHelper implements AdsMogoListener {
	private static final String mogoID = "a6958f65063b45c09ac0615cd6638f84";
	private static ADHelper Instance = new ADHelper();
	private static AdsMogoLayout adMogoLayoutCode;
	private static Object syncObj = new Object();
	private static boolean isShowed = false;

	private ADHelper() {
	}

	public static void initAD() {
		MainActivity.instance.runOnUiThread(new Runnable() {
			@Override
			public void run() {
				synchronized (syncObj) {
					if (!StageManager.instance.isBuyNoAD()
							&& AppControl.isShowAd() && !isShowed) {
						isShowed = true;
						adMogoLayoutCode = new AdsMogoLayout(
								MainActivity.instance, mogoID, false);
						FrameLayout.LayoutParams params = new FrameLayout.LayoutParams(
								FrameLayout.LayoutParams.FILL_PARENT,
								FrameLayout.LayoutParams.WRAP_CONTENT);
						// 设置广告出现的位置(悬浮于底部)
						params.gravity = Gravity.BOTTOM;
						params.bottomMargin = 0;
						// 添加广告状态监听
						adMogoLayoutCode.setAdsMogoListener(ADHelper.Instance);
						MainActivity.instance.addContentView(adMogoLayoutCode,
								params);
						hideAd();
					}
				}
			}
		});
	}

	public static void showAd() {
		if (adMogoLayoutCode != null) {
			MainActivity.instance.runOnUiThread(new Runnable() {
				@Override
				public void run() {
					adMogoLayoutCode.setVisibility(View.VISIBLE);
				}
			});
		}
	}

	public static void hideAd() {
		if (adMogoLayoutCode != null) {
			MainActivity.instance.runOnUiThread(new Runnable() {
				@Override
				public void run() {
					adMogoLayoutCode.setVisibility(View.INVISIBLE);
				}
			});
		}
	}

	@Override
	public Class getCustomEvemtPlatformAdapterClass(
			AdsMogoCustomEventPlatformEnum arg0) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public void onClickAd(String arg0) {
		// 发生点击,暂停游戏
		MainActivity.instance.runOnUiThread(new Runnable() {

			@Override
			public void run() {
				GameScene.make().pauseGame();
			}
		});
	}

	@Override
	public void onCloseMogoDialog() {
		// TODO Auto-generated method stub

	}

	@Override
	public void onFailedReceiveAd() {
		// TODO Auto-generated method stub

	}

	@Override
	public void onRealClickAd() {
		// TODO Auto-generated method stub

	}

	@Override
	public void onReceiveAd(ViewGroup arg0, String arg1) {
		// TODO Auto-generated method stub

	}

	@Override
	public void onRequestAd(String arg0) {
		// TODO Auto-generated method stub

	}

	@Override
	public boolean onCloseAd() {
		// TODO Auto-generated method stub
		return false;
	}

}
